<template>
  <div id="home">
    <nav-bar class="home_nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control :titles="['流行','新款','精选']"
                 @tabClick="tabClick"
                 ref="tabControl1"
                 class="tab_control" v-show="isTabFixed">
    </tab-control>
    <scroll class="content"
            ref="scroll"
            :probe-type="3"
            @scroll="contentScroll"
            :pull-up-load="true"
            @pullingUp="loadMore">
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"></home-swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control :titles="['流行','新款','精选']"
                   @tabClick="tabClick"
                   ref="tabControl2">
      </tab-control>
      <!--<good-list :goods="goods[currentType].list"></good-list>-->
      <good-list :goods="showGoods"></good-list>
    </scroll>

    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
    <!--.native监听组件根元素的原生事件   click.native监听点击事件  Vue3.0可不加-->

  </div>
</template>

<script>
import HomeSwiper from "@/views/home/childComps/HomeSwiper";
import RecommendView from "@/views/home/childComps/RecommendView";
import FeatureView from "@/views/home/childComps/FeatureView";

import NavBar from "@/components/common/navbar/NavBar";
import TabControl from "@/components/content/tabControl/TabControl";
import GoodList from "@/components/content/goods/GoodList";
import GoodsListItem from "@/components/content/goods/GoodsListItem";
import Scroll from "@/components/common/scroll/Scroll";
import BackTop from "@/components/content/backTop/BackTop";

import {getHomeMultidata, getHomeGoods} from "@/network/home";
import {itemListenerMixin} from "@/common/mixin";
import {debounce} from "@/common/utils";

export default {
  name: "Home",
  components: {
    NavBar,
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodList,
    GoodsListItem,
    Scroll,
    BackTop,
  },

  data() {
    return {
      // result: null
      //result保存请求过来的数据
      banners: [],
      recommends: [],
      goods: {
        'pop': {page: 0, list: []},
        'new': {page: 0, list: []},
        'sell': {page: 0, list: []},
      },
      currentType: 'pop',
      isShowBackTop: false,
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    }
  },
  mixins: [itemListenerMixin],
  computed: {
    showGoods() {
      return this.goods[this.currentType].list
    }
  },
  //created()会在组件创建完会调用
  //具体代码抽到methods里面，created写具体逻辑
  created() {
    //1.请求多个数据
    this.getHomeMultidata();
    // getHomeMultidata().then(res => {
    //   // console.log(res);
    //   this.result = res;
    //   this.banners = res.data.banner.list;
    //   this.recommends = res.data.recommend.list;
    // })


    //2.请求商品数据
    this.getHomeGoods('pop');
    this.getHomeGoods('new');
    this.getHomeGoods('sell');


  },
  mounted() {
    //3.监听item中图片加载完成
    // this.$bus.$on('itemImageLoad', () => {
    //监听每一张图片是否加载完成,只要有一张图片加载完成了,就执行一次refresh()
    // this.$refs.scroll.scroll.refresh();

    // this.$refs.scroll.refresh();
    // })


    let refresh = debounce(this.$refs.scroll.refresh, 200)
    //
    // //对监听的时间进行保存
    this.itemImgListener = () => {
      refresh();
    }
    this.$bus.$on('itemImageLoad', this.itemImgListener);


    //获取tabControl的offsetTop
    //所有的组件都有一个属性$el:用于获取组件中的元素
    // console.log(this.$refs.tabControl.$el.offsetTop);
    // this.tabOffsetTop = this.$refs.tabControl.offsetTop
  },
  destroyed() {
    //keep-alive可以解决
    // console.log('home界面被销毁~');
  },
  activated() {
    // console.log('activated');     //进入home页调用

    this.$refs.scroll.scrollTo(0, this.saveY, 0);
    this.$refs.scroll.refresh();
  },
  deactivated() {
    // console.log('deactivated');   //离开home页调用

    //1.保存Y值
    this.saveY = this.$refs.scroll.getScrollY();

    //2.取消全局事件监听
    this.$bus.$off('itemImageLoad', this.itemImgListener);
  },
  methods: {
    //事件监听相关方法

    tabClick(index) {
      // console.log(index);
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
      if (this.$refs.tabContol1 !== undefined) {
        this.$refs.tabContol1.currentIndex = index;
        this.$refs.tabContol2.currentIndex = index;
      }
    },
    backClick() {
      //运用ref="scroll"   访问scroll组件
      // this.$refs.scroll.scroll.scrollTo(0,0,3000);
      this.$refs.scroll.scrollTo(0, 0, 1000)
    },
    contentScroll(position) {
      // console.log(position);
      //1.判断BackTop是否显示
      this.isShowBackTop = (-position.y) > 1000;

      //2.决定tabControl是否吸顶(position:fixed)
      this.isTabFixed = (-position.y) > this.tabOffsetTop;

    },
    loadMore() {
      // console.log('加载更多~');
      this.getHomeGoods(this.currentType);
    },
    swiperImageLoad() {
      // console.log(this.$refs.tabControl.$el.offsetTop);
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },


    //网络请求相关的方法
    getHomeMultidata() {
      getHomeMultidata().then(res => {
        // console.log(res);
        this.result = res;
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      })
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;

      getHomeGoods(type, page).then(res => {
        // console.log(res);
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;

        //完成上拉加载更多
        this.$refs.scroll.finishPullUp();
      })
    }
  },

}
</script>

<style scoped>
/*scoped将样式限制于此组件内*/
#home {
  position: relative;
  /*padding-top: 44px;*/
  height: 100vh;
  /*vh = view height视口高度*/
}

.home_nav {
  /*浏览器原生滚动时，使导航栏不随着一起滚动时   用Better-scroll不会出现此问题*/
  /*position: fixed;*/
  /*left: 0;*/
  /*right: 0;*/
  /*top: 0;*/
  /*z-index: 9;*/
  background-color: var(--color-tint);
  color: #ffffff;
}

.content {
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  overflow: hidden;
}

/*.content {*/
/*  height: calc(100% - 93px);*/
/*  overflow: hidden;*/
/*  margin-top: 44px;*/
/*}*/

.tab_control {
  position: relative;
  z-index: 9;
}

</style>
